package com.gyd.marte;

import java.text.MessageFormat;

import com.gyd.sqlite.UserSQLiteHelper;
import com.gyd.utils.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MarteActivity extends Activity {

	EditText username;
	EditText password;
	TextView error;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_marte);
		conectar(this);
		error = (TextView) findViewById(R.id.errorlogin);
		error.setVisibility(error.INVISIBLE);
		username = (EditText) findViewById(R.id.userName);
		password = (EditText) findViewById(R.id.pass);
		Log.i("INFOTAG", "getting properties of button submit");
		buttons();
	}

	public void buttons() {
		Button submit = (Button) this.findViewById(R.id.submit);
		submit.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Log.i("INFOTAG",
						"opening a new view with the page of welcome!! ");

				if(password.getText().toString().length()<1){
					error.setVisibility(error.VISIBLE);
				}
				Cursor curs = getTodosLosUsuarios("User", username.getText()
						.toString(),password.getText().toString());
				if (curs.moveToFirst()
						&& Utils.isString(username.getText().toString())) {
					error.setVisibility(error.INVISIBLE);

					Log.i("INFOTAG", "si hay datos, logeo exitoso");
					lanzar(arg0);
				} else {
					error.setVisibility(error.VISIBLE);
					Log.i("INFOTAG", "no hay datos, logeo no exitoso");
				}
			}

		});

		Button registro = (Button) this.findViewById(R.id.registro);
		registro.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Log.i("INFOTAG",
						"opening a new view with the page of subscription!! ");
				registro(arg0);
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_marte, menu);
		return true;
	}

	public void registro(View view) {
		// Intent i = new Intent(this, InitialActivity.class );
		Intent i = new Intent(this, registroActivity.class);
		startActivity(i);
	}

	public void lanzar(View view) {
		// Intent i = new Intent(this, InitialActivity.class );
		Intent i = new Intent(this, Welcome.class);
		startActivity(i);
	}

	UserSQLiteHelper usdbh;
	SQLiteDatabase db;

	public void conectar(Context contexto) {

		try {
			Log.i("INFOTAG", "tratando de obtener la conexion");
			usdbh = new UserSQLiteHelper(contexto, "DBUsuarios", null, 1);
			Log.i("INFOTAG", "salida obteniendo la conexion");
			db = usdbh.getWritableDatabase();
			Log.i("INFOTAG", "se obtuvo la conexion");
		} catch (Exception e) {
			Log.e("ERRORTAG", "Exception in inserting data  " + e);
		}
	}

	public void lectura() {
		db = usdbh.getReadableDatabase();
	}

	public Cursor getTodosLosUsuarios(String table, String userName,
			String password) {
		this.lectura();
		String query = MessageFormat.format(
				"SELECT * from {0} WHERE nombre={1}  ", "'" + table
						+ "'", "'" + userName + "'");
		Log.i("INFOTAG", query);
		Cursor cur = db.rawQuery(query, new String[] {});
		return cur;
	}
}
